package com.gsy.controller.admin;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsy.aop.annotation.TokenValid;
import com.gsy.entity.ClassesEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.response.BaseResponse;
import com.gsy.service.ClassesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-06
 */
@RestController
@RequestMapping(value = "/admin", consumes = "application/json", produces = "application/json")
@Api(value = "controller", tags = {"tb_classes 操作接口"})
public class ClassesController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ClassesController.class);

    @Resource
    private ClassesService classesService;

//    /**
//    * 更新
//    * @param request
//    * @return
//    */
//    @ApiOperation(value = "更新tb_classes信息", notes = "根据请求参数信息更新tb_classes")
//    @PutMapping("classes")
//    @ParameterValid(apiName = "更新tb_classes信息", formatObj = UpdateClassesRequest.class)
//    public BaseResponse updateClassesInformation(@RequestBody UpdateClassesRequest request) {
//
//        ClassesEntity entity = classesService.getById(request.getId());
//        if(entity == null){
//            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
//        }
//        BeanUtils.copyProperties(request, entity);
//
//        Boolean flag = classesService.updateById(entity);
//        if (flag) {
//            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode());
//        } else {
//            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "更新失败！");
//        }
//    }
//
//
//    /**
//    * 删除tb_classes信息
//    * @param request
//    * @return
//    */
//    @ApiOperation(value = "删除tb_classes信息", notes = "根据请求参数信息删除tb_classes信息")
//    @DeleteMapping("classes/{id}")
//    public BaseResponse deleteClasses(@PathVariable Integer id) {
//    ClassesEntity entity = classesService.getById(request.getId());
//
//        if (entity == null) {
//            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
//        }
//
//        classesService.removeById(request.getId());
//        return new BaseResponse(SystemStatusEnum.SUCCESS.getCode());
//
//    }


    /**
    * 新增
    * @param classes
    * @return
    */
    @ApiOperation(value = "新增tb_classes信息", notes = "根据请求参数信息新增tb_classes")
    @GetMapping("addClasses/{classes}")
    public BaseResponse addClasses(@PathVariable String classes) {

        ClassesEntity entity = classesService.getOne(Wrappers.lambdaQuery(new ClassesEntity()).eq(ClassesEntity::getClasses, classes));
        if (entity != null){
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "重复添加！");
        }
        entity = new ClassesEntity();
        entity.setClasses(classes);
        boolean flag = classesService.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "新增失败！");
        }
    }


    /**
    * 获取列表
    * @return
    */
    @ApiOperation(value = "获取tb_classes列表", notes = "根据请求参数搜索获取tb_classes列表")
    @GetMapping("getClasses")
    public BaseResponse getClassesList() {

        List<ClassesEntity> entities = classesService.list();

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }


    /**
    * 模糊查询班级(分页获取数据与数量)
    * @return
    */
    @ApiOperation(value = "模糊查询班级(分页获取数据与数量)", notes = "模糊查询班级(分页获取数据与数量)")
    @GetMapping("getClassesSear/{inputText}/{currPage}/{pageNum}")
    @TokenValid
    public BaseResponse getClassesSear(@PathVariable String inputText,@PathVariable Integer pageNum,@PathVariable Integer currPage) {

        Page<ClassesEntity> page = new Page<>(currPage, pageNum);
        Page<ClassesEntity> entities = classesService.page(page,
                Wrappers.lambdaQuery(new ClassesEntity()).like(ClassesEntity::getClasses,
                        inputText));
        List<ClassesEntity> records = entities.getRecords();

        if (CollectionUtil.isNotEmpty(records)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            success.setTotal(page.getTotal());
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }

}
